package TestPaperSystem.service.impl;

import TestPaperSystem.enmus.QuType;
import TestPaperSystem.entity.Answer;
import TestPaperSystem.entity.ExamPaperQu;
import TestPaperSystem.mapper.ExamPaperQuMapper;
import TestPaperSystem.service.AnswerService;
import TestPaperSystem.service.ExamPaperQuService;
import TestPaperSystem.utils.ThreadPool;
import TestPaperSystem.utils.WordImpl;
import TestPaperSystem.utils.compiler.StringCompiler;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@Service
public class ExamPaperQuServiceImpl extends ServiceImpl<ExamPaperQuMapper, ExamPaperQu> implements ExamPaperQuService {

    @Resource
    ExamPaperQuMapper examPaperQuMapper;

    @Autowired
    AnswerService answerService;

    @Override
    public boolean updateByKeys(ExamPaperQu examPaperQu) {
        UpdateWrapper<ExamPaperQu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ExamPaperQu::getPaperId,examPaperQu.getPaperId())
                .eq(ExamPaperQu::getQuId , examPaperQu.getQuId());
        return examPaperQuMapper.update(examPaperQu , updateWrapper) > 0;
    }

    //计算客观题分数
    @Override
    public int claObjScore(String paperId) {
        List<ExamPaperQu> list = examPaperQuMapper.selectList(new QueryWrapper<ExamPaperQu>().lambda()
                .eq(ExamPaperQu::getPaperId,paperId)
                .le(ExamPaperQu::getQuType,3)
                .eq(ExamPaperQu::getIsRight,true)
                .eq(ExamPaperQu::getAnswered,true));

        return list.stream().mapToInt(ExamPaperQu::getActualScore).sum();
    }

    //计算主观题分数,辅助使用
    @Override
    public int claSubjScore(String paperId) {

        int total = 0;

        List<ExamPaperQu> list = examPaperQuMapper.selectList(new QueryWrapper<ExamPaperQu>().lambda()
                .eq(ExamPaperQu::getPaperId,paperId)
                .ge(ExamPaperQu::getQuType,4)
                .eq(ExamPaperQu::getAnswered,true));

        List<ExamPaperQu> list_blanks = list.stream().filter(e -> e.getQuType() == QuType.BLANK).collect(Collectors.toList());

        List<ExamPaperQu> list_programs = list.stream().filter(e -> e.getQuType() == QuType.PROGRAM).collect(Collectors.toList());

        if(list_blanks.isEmpty() && list_programs.isEmpty())
            return 0;


        for(ExamPaperQu item :list_blanks){
            Answer answer = answerService.getAnswerByPaperQuAnswer(item.getPaperId() , item.getQuId());
            int round = Math.round(WordImpl.getString(item.getAnswer(), answer.getContent()) * item.getScore());
            total += round;
            item.setActualScore(round);
        }
        for(ExamPaperQu item : list_programs){
            Answer answer = answerService.getAnswerByPaperQuAnswer(item.getPaperId() , item.getQuId());
            //待处理
            int round = Math.round( StringCompiler.getInstance().actualScore("Main.java",item.getAnswer() , answer.getAnalysis() ,answer.getContent())
                    * item.getScore());
            total += round;
            item.setActualScore(round);
        }
        updateMsg(list_blanks);
        updateMsg(list_programs);
        return total;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateMsg(List<ExamPaperQu> list) {
        this.updateBatchById(list);
    }

}
